import slash from 'slash2';
import { version } from 'moment';
import defaultSettings from './defaultSettings';
import routes from './routes';
import webpackPlugin from './plugin.config';
import theme from './theme';

const { pwa, primaryColor } = defaultSettings; // preview.pro.ant.design only do not use in your production ;
// preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。

const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;
const isAntDesignProPreview = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site';
const plugins = [
  [
    'umi-plugin-react',
    {
      antd: true,
      dva: {
        hmr: true,
      },
      locale: {
        // default false
        enable: false,
        // default zh-CN
        default: 'zh-CN',
        // default true, when it is true, will use `navigator.language` overwrite default
        baseNavigator: true,
      },
      dynamicImport: {
        loadingComponent: './components/PageLoading/index',
        webpackChunkName: true,
        level: 3,
      },
      pwa: pwa
        ? {
            workboxPluginMode: 'InjectManifest',
            workboxOptions: {
              importWorkboxFrom: 'local',
            },
          }
        : false,
      // default close dll, because issue https://github.com/ant-design/ant-design-pro/issues/4665
      // dll features https://webpack.js.org/plugins/dll-plugin/
      // dll: {
      //   include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
      //   exclude: ['@babel/runtime', 'netlify-lambda'],
      // },
    },
  ],
  [
    'umi-plugin-pro-block',
    {
      moveMock: false,
      moveService: false,
      modifyRequest: true,
      autoAddMenu: true,
    },
  ],
];

export default {
  plugins,
  block: {
    // 国内用户可以使用码云
    defaultGitUrl: 'https://gitee.com/ant-design/pro-blocks',
    // defaultGitUrl: 'https://github.com/ant-design/pro-blocks',
  },
  history: 'hash',
  hash: true,
  targets: {
    ie: 11,
  },
  devtool: isAntDesignProPreview ? 'source-map' : false,
  routes,
  theme: {
    'primary-color': primaryColor,
    ...theme,
  },
  define: {
    'process.env.loginUrl': 'http://marstest.cifi.com.cn:8080/login',
    'process.env.apiUrl': 'http://marstest.cifi.com.cn:8080',
    'process.env.oaUrl': 'https://oadev.cifi.com.cn/front/oa.helper.js',
    'process.env.logoutUrl': 'https://sso-uat.cifi.com.cn/siam/logout',
    'process.env.smartBI_table2':
      'https://smartbi-test.cifi.com.cn/smartbi/vision/openresource.jsp?resid=I8a01a54e0170f66ff66fc6af0170f6f946d225cd',
    'process.env.smartBI_table3':
      'https://smartbi-test.cifi.com.cn/smartbi/vision/openresource.jsp?resid=I8a01a54e0170fc27fc2777f90171074b04b371f7',
    'process.env.smartBI_table5':
      'https://smartbi-test.cifi.com.cn/smartbi/vision/openresource.jsp?resid=I8a01a54e0170fc27fc2777f901710b99e9121b25',
    'process.env.protal': 'https://portal-test.cifi.com.cn/workflow?tab=0&dd_nav_bgcolor=FF2761FF',
    ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION:
      ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION || '', // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。
  },
  ignoreMomentLocale: true,
  lessLoaderOptions: {
    javascriptEnabled: true,
  },
  disableRedirectHoist: true,
  cssLoaderOptions: {
    modules: true,
    getLocalIdent: (context, _, localName) => {
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }

      const match = context.resourcePath.match(/src(.*)/);

      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
          .map(a => a.replace(/([A-Z])/g, '-$1'))
          .map(a => a.toLowerCase());
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }

      return localName;
    },
  },
  manifest: {
    basePath: '/',
  },
  chainWebpack: webpackPlugin,
};
